<?php

namespace App\Dao;

use App\Model\BillsModel;

class BillsDao extends BaseDao
{
    public static function builder_query($params = []): \Illuminate\Database\Eloquent\Builder
    {
        $query= BillsModel::query();

        $query->when(checkIsHasField($params,'user_id'),function ($query)use ($params){
            $query->where('bill_user_id',$params['user_id']);
        });

        $query->when(checkIsHasField($params,'type'),function ($query)use ($params){
            $query->where('type',$params['type']);
        });


        return $query->orderByDesc('created_at');
    }


    public static function moneyUpdate($money = 0.00,$user_id = 0,$title = '',$type = 0){
        self::builder_query()->create([
            'bill_price'=>$money,
            'bill_user_id'=>$user_id,
            'title'=>$title,
            'type'=>$type
        ]);
        UsersDao::builder_query([
            'user_id'=>$user_id
        ])->increment('u_price',$money);
    }

    public static function bills_page(array $params): \Illuminate\Contracts\Pagination\LengthAwarePaginator
    {
        return self::builder_query($params)->paginate();
    }
}
